package cn.xp.service.impl;

import cn.xp.domain.Device;
import cn.xp.domain.Floor;
import cn.xp.dto.DeviceDto;
import cn.xp.dto.DeviceTypeDto;
import cn.xp.dto.FloorDto;
import cn.xp.enums.DeviceEnums;
import cn.xp.mapper.DeviceMapper;
import cn.xp.service.IDeviceService;
import cn.xp.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceService implements IDeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public Device findOne(Long id) {
        return deviceMapper.findOne(id);
    }

    @Override
    public List<DeviceDto> getDeviceDto(Long id) {
        List<Device> all = deviceMapper.findAll();
        List<DeviceDto> deviceDtos = new ArrayList<>();
        for (Device device : all) {
            DeviceDto deviceDto = new DeviceDto();
            deviceDto.setName(device.getName());
//            deviceDto.setType(device.getType());
            deviceDtos.add(deviceDto);
        }
        return deviceDtos;
    }

    @Override
    public List<Device> findAll() {
        return deviceMapper.findAll();
    }

    @Override
    public JSONResult<Device> add(Device device) {
        deviceMapper.add(device);
        return JSONResult.success();
    }

    /**
     * 根据设备id查询设备信息
     *
     * @param id
     * @return
     */
    @Override
    public JSONResult<DeviceDto> getDeviceInfoById(Long id) {
        Device device = deviceMapper.findOne(id);
        DeviceDto deviceDto = new DeviceDto();
        Long type = device.getType();
        if (type == 1) {
            deviceDto.setType(DeviceEnums.DEVICE_TYPE_SMOKE);
        }
        if (type == 2) {
            deviceDto.setType(DeviceEnums.DEVICE_TYPE_REPORT);
        }
        if (type == 3) {
            deviceDto.setType(DeviceEnums.DEVICE_TYPE_CAMERA);
        }
        deviceDto.setName(device.getName());
        deviceDto.setBuildId(device.getBuildId());
        deviceDto.setFloorId(device.getFloorId());
        return JSONResult.success(deviceDto);
    }

    /**
     * 根据楼宇id查询设备
     *
     * @param id
     * @return
     */
    @Override
    public List<DeviceTypeDto> DeviceType(Long id) {

        List<Device> allDeviceType = deviceMapper.findAllDeviceType(id);
        ArrayList<DeviceTypeDto> list = new ArrayList<>();

        for (Device device : allDeviceType) {

            DeviceTypeDto deviceTypeDto = new DeviceTypeDto();
            if (device.getType() == 1) {
                deviceTypeDto.setType(DeviceEnums.DEVICE_TYPE_SMOKE);
            }
            if (device.getType() == 2) {
                deviceTypeDto.setType(DeviceEnums.DEVICE_TYPE_REPORT);
            }
            if (device.getType() == 3) {
                deviceTypeDto.setType(DeviceEnums.DEVICE_TYPE_CAMERA);
            }
            deviceTypeDto.setName(device.getName());
            list.add(deviceTypeDto);
        }
        return list;
    }

    /**
     * 根据设备类型 查找所有的设备
     *
     * @param id
     * @return
     */
    @Override
    public List<DeviceTypeDto> TypeList(Long id) {
        List<Device> type = deviceMapper.findType(id);
        ArrayList<DeviceTypeDto> arrayList = new ArrayList<>();
        for (Device device : type) {
            DeviceTypeDto deviceTypeDto = new DeviceTypeDto();
            deviceTypeDto.setName(device.getName());
            if (device.getType() == 1) {
                deviceTypeDto.setType(DeviceEnums.DEVICE_TYPE_SMOKE);
            }
            if (device.getType() == 2) {
                deviceTypeDto.setType(DeviceEnums.DEVICE_TYPE_REPORT);
            }
            if (device.getType() == 3) {
                deviceTypeDto.setType(DeviceEnums.DEVICE_TYPE_CAMERA);
            }
            arrayList.add(deviceTypeDto);
        }
        return arrayList;
    }
}
